package com.ecoolex.hotel.saas.core.manager;

import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.hotel.saas.core.dao.ActivityShowStoreDAO;
import com.ecoolex.hotel.saas.entity.ActivityShowStore;
import com.ecoolex.hotel.saas.entity.ActivityShowStoreExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.Date;
import java.util.List;

@Service
public class ActivityShowStoreManager {

    @Autowired
    private ActivityShowStoreDAO dao;

    public ActivityShowStore getActivityShowStore(Integer id) {
        return dao.selectByPrimaryKeyWithLogicalDelete(id, ActivityShowStore.NOT_DELETED);
    }

    public Pagination<ActivityShowStore> queryActivityShowStore(String storeName, Integer activityId, Page page) {
        return Pagination.create(page, dao.count(storeName, activityId), () -> dao.query(storeName, activityId, page));
    }

    public List<ActivityShowStore> queryByActivityId(Integer activityId) {
        ActivityShowStoreExample example = new ActivityShowStoreExample();
        ActivityShowStoreExample.Criteria criteria = example.createCriteria();
        criteria.andActivityIdEqualTo(activityId).andDeletedEqualTo(ActivityShowStore.NOT_DELETED);
        return dao.selectByExample(example);
    }

    public void addActivityShowStore(Integer activityId, List<Integer> storeIdList, String createUser) {
        for (Integer storeId : storeIdList) {
            if (isRelationActivityShow(activityId, storeId)) {
                continue;
            }
            ActivityShowStore record = new ActivityShowStore();
            record.setActivityId(activityId);
            record.setStoreId(storeId);
            record.setDeleted(ActivityShowStore.NOT_DELETED);
            record.setCreateUser(createUser);
            record.setCreateTime(Date.from(Instant.now()));
            record.setUpdateUser(createUser);
            record.setUpdateTime(Date.from(Instant.now()));
            dao.insert(record);
        }
    }

    public void delActivityShowStore(ActivityShowStore record, String updateUser) {
        record.setDeleted(ActivityShowStore.IS_DELETED);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(record, ActivityShowStore.Column.deleted, ActivityShowStore.Column.updateUser, ActivityShowStore.Column.updateTime);
    }

    public void deleteByActivityId(Integer activityId, String updateUser) {
        List<ActivityShowStore> list = queryByActivityId(activityId);
        list.forEach(activityShowStore -> {
            delActivityShowStore(activityShowStore, updateUser);
        });
    }

    public Boolean isRelationActivityShow(Integer activityId, Integer storeId) {
        ActivityShowStoreExample example = new ActivityShowStoreExample();
        ActivityShowStoreExample.Criteria criteria = example.createCriteria();
        criteria.andActivityIdEqualTo(activityId).andStoreIdEqualTo(storeId).andDeletedEqualTo(ActivityShowStore.NOT_DELETED);
        return Check.notNull(dao.selectOneByExample(example));
    }
}